package com.shiliao.app.splash;

import android.content.Intent;
import android.os.Handler;

import com.shiliao.app.R;
import com.shiliao.app.activity.MainActivity;
import com.shiliao.app.manager.NetworkManager;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.FileNameManage;
import com.shiliao.app.utils.YMServerUtil;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.yimei.framework.base.ui.BaseActivity;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;
import com.yimei.framework.util.PreferenceHelper;
import com.yimei.framework.util.SystemUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by yx on 2017/6/2.
 * 首页启动页
 */

public class WelcomeActivity extends BaseActivity implements WelcomeView {
    private boolean bGotuserid = false;
    private Map param;
    private String device;

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {
        param = new HashMap();
        //返回首页
//        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
//        intent.putExtra("pos", 0);
//        toActivity(intent);
        //   finish();
        WelcomePresenter welcomePresenter = new WelcomePresenter(this);
        String local_userId = YMUserManager.getUserId(this);
        String cellphone = SystemUtils.getNativePhoneNumber(this);
        if (local_userId.equals("0")) {
            if (cellphone.length() == 11) {
                param.put("cellphone_no", cellphone);
                param.put("device_id", "");
                param.put("os_type", "Android");
                welcomePresenter.resquestUserId(param, this);
            } else {
                double r = Math.random();
                r = r * 100000000;
                int i = (int) r;
                device = String.valueOf(i);
                param.put("cellphone_no", "");
                param.put("device_id", device);
                param.put("os_type", "Android");
                welcomePresenter.resquestUserId(param, this);
            }

        }

//        getUserId();
        welcomePresenter.request(YMUserManager.getUserId(this));
        welcomePresenter.requestIsExamine(YMUserManager.getUserId(this));

        XGPushManager.registerPush(this,YMUserManager.getUserId(this),
                new XGIOperateCallback() {
                    @Override
                    public void onSuccess(Object data, int flag) {
                        LogUtil.d("TPush", "注册成功，设备token为：" + data);
                    }

                    @Override
                    public void onFail(Object data, int errCode, String msg) {
                        LogUtil.d("TPush", "注册失败，错误码：" + errCode + ",错误信息：" + msg);
                    }
                });
        Handler handler = new Handler(getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                int waittime = 0;
//                while(!bGotuserid && waittime<3) //最多再等 3 秒
//                {
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    waittime++;
//                }
                Intent intent = new Intent();
//                intent.setClass(WelcomeActivity.this, IndexActivity.class);
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                WelcomeActivity.this.finish();
            }
        }, 3000);

    }

    private void getUserId() {
        String local_userId = YMUserManager.getUserId(this);
        String device;
        String cellphone = SystemUtils.getNativePhoneNumber(this);
        Map param = new HashMap();

        if (local_userId.equals("0")) {
            if (cellphone.length() == 11) {
                param.put("device_id", "");
                param.put("cellphone_no", cellphone);
            } else {
                double r = Math.random();
                r = r * 100000000;
                int i = (int) r;
                device = String.valueOf(i);

                param.put("device_id", device);
                param.put("cellphone_no", "");
            }
            param.put("os_type","Android");

            request(YMServerUtil.GET_USER_ID, param, new ICallBack() {
                @Override
                public void onSuccess(int statusCode, Object result) {
                    LogUtil.e("yzq", result.toString());
                    String userId = ParseHelper.getString(result.toString(), "user_id");
                    LogUtil.e("yzq", userId);
                    YMUserManager.saveUserId(WelcomeActivity.this, userId);//保存用户id
                    bGotuserid = true;
                }

                @Override
                public void onFail(int statusCode, Object result) {
                    LogUtil.e("yzq", result.toString());
                }
            });
        } else {
            bGotuserid = true;
        }

    }

    private void request(String url, Map map, ICallBack iCallBack) {
        try {
//            NetworkManager.loadDataByPost(url, map, iCallBack);
            NetworkManager.loadRequsetByRaw(url, map, iCallBack);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    public void getGuidance(final String share, final String infosetting, final String dailyremind) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                PreferenceHelper.write(WelcomeActivity.this, FileNameManage.WELCOME_PAGE,
                        FileNameManage.WELCOME_PAGE_SHARE_GUID, share);//存储分享指导

                PreferenceHelper.write(WelcomeActivity.this, FileNameManage.WELCOME_PAGE,
                        FileNameManage.WELCOME_PAGE_INFOSETTING_GUID, infosetting);//存储设置指导

                PreferenceHelper.write(WelcomeActivity.this, FileNameManage.WELCOME_PAGE,
                        FileNameManage.WELCOME_PAGE_IDAILYREMIND_GUID, dailyremind);//存储提醒指导
            }
        });
    }

    @Override
    public void isAuditor(String isAuditor) {
        PreferenceHelper.write(this,FileNameManage.WELCOME_PAGE,FileNameManage.WELCOME_PAGE_IS_AUDITOR,isAuditor);
    }


}
